import { Component, OnInit, Injector, ViewChild, Output, EventEmitter } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    EditDtoExtensionsItem,
    Stock_BaseFieldListDto,
    StockZT_RoomServiceProxy,
    StockZT_LocationServiceProxy,
    StockZT_IntoBillItemServiceProxy,
    Stock_IntoMarketItemEditDto
} from '@shared/service-proxies/stock-service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import { UserDataPermissionServiceProxy } from '@shared/service-proxies/stock-service-proxies';

@Component({
    selector: 'createOrEditMarketIntoBillItemModal',
    templateUrl: './create-or-edit-market-into-bill-item-modal.component.html'
})
export class CreateOrEditMarketIntoBillItemModalComponent extends AppComponentBase implements OnInit {
    constructor(
        injector: Injector,
        private _billService: StockZT_IntoBillItemServiceProxy,
    ) {
        super(injector);
    }

    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Output() clearQuery: EventEmitter<any> = new EventEmitter<any>();
    active = false;
    saving = false;

    editDto: Stock_IntoMarketItemEditDto;
    extensions: Array<EditDtoExtensionsItem>;
    roomOption;
    insertid;
    optionAll;

    selecttept;
    ngOnInit() {}

    show(billId: string, id?: string, basefieldinfo?: Stock_BaseFieldListDto, insertid?: string): void {
        if (!billId) {
            this.notify.error('未查询到入库单');
            return;
        }
        if (!basefieldinfo) {
            this.notify.error('未查询到基础信息');
            return;
        }

        this.active = true;

        this.insertid = insertid;
        this._billService.getIntoMarketItemForEditExtension(id).subscribe(result => {
            let optionAll = {};
            if (result.selects) {
                result.selects.forEach(el => {
                    optionAll[el.label] = el.value;
                });
            }

            this.optionAll = optionAll;
            this.optionAll['f_WarehouseOptions'] = result.selects[0].value.map(e => {
                return {
                    label: e.label,
                    value: e
                };
            });

            this.optionAll['list_PR_DeptOptions']= result.depts.map(e=>{
                return {
                    label:e.displayText,
                    value:e
                }
            })

            this.editDto = result.dto;

            this.editDto.f_BillId = billId;
            this.editDto.f_Warehouse = result.selects[0].value[0];
            if (id == null) {
                this.editDto.f_MaterialCode = basefieldinfo.f_MaterialCode;
                this.editDto.f_MaterialName = basefieldinfo.f_MaterialName;
                this.editDto.f_Unit = basefieldinfo.f_UnitId;
                this.editDto.f_UnitPrice = basefieldinfo.f_Price;
                this.editDto.f_Station = basefieldinfo.f_Station;
                this.editDto.f_Size = basefieldinfo.f_Size;
                this.editDto.f_Trademark = basefieldinfo.f_TradeMark;
                this.editDto.f_TecCondition = basefieldinfo.f_TecCondition;
                this.editDto.f_Specification = basefieldinfo.f_Specification;    
                this.editDto.f_QualityLevel = basefieldinfo.f_QualityLevel;            
            }
            console.log(this.editDto);
            this.extensions = result.items;
            this.modal.show();
        });
    }

    save(): void {
        this.saving = true;

        console.log(this.editDto);
        if (this.insertid) {
            this.editDto.id = this.insertid;
            this._billService
                .insertMarketIntoItem(this.editDto)
                .pipe(
                    finalize(() => {
                        this.saving = false;
                    })
                )
                .subscribe(() => {
                    this.notify.info(this.l('插入成功！'));
                    this.close();
                    this.clearQuery.emit();
                    this.modalSave.emit(null);
                });
        } else {
            this._billService
                .createOrUpdateIntoMarketItem(this.editDto)
                .pipe(
                    finalize(() => {
                        this.saving = false;
                    })
                )
                .subscribe(() => {
                    this.notify.info(this.l('SavedSuccessfully'));
                    this.close();
                    this.clearQuery.emit();
                    this.modalSave.emit(null);
                });
        }
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
